package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import dicts.RealmDict;
import entities.TLogic;
import interfaces.ILogicService;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.LogUtil;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;
import views.VLogicTransaction;

public class LogicAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ILogicService logicService;

	private TLogic logicBean;
	private List<TLogic> logicList;

	private VLogicTransaction logicTransactionBean;
	private List<VLogicTransaction> logicTransactionList;

	private String logNo;
	private String logNos;
	private String txnNo;
	private String txnNos;
	private String logName;
	private String logOrder;
	private String logState;
	private Date logDate;
	private Date logDateTop;
	private Date logDateEnd;
	private String logRemark;
	private String crdNo;
	private String merNo;
	private String txnName;
	private String txnOrder;
	private String txnSite;
	private String txnProduct;
	private String txnNotify;
	private String txnCur;
	private Double txnLoc;
	private Double txnRate;
	private Double txnAmount;
	private Double txnRemain;
	private String txnRefund;
	private String txnRefuse;
	private String txnFreeze;
	private String txnLogic;
	private String txnSettle;
	private String txnDanger;
	private String txnVisible;
	private String txnState;
	private Date txnDate;
	private Date txnDateTop;
	private Date txnDateEnd;
	private String txnRemark;


	// 打回
	public String savebatch() throws Exception {
		JSONObject resultMap = new JSONObject();
		Map<String, String> paramsMap = new HashMap<String, String>();
		
		if (StringUtil.isEmpty(logNos)) {
			addActionError("未选中任何要驳回的物流数据");
			return INFO;
		}
		
		String[] contents = logNos.split(",");
		if (contents.length == 0) {
			addActionError("数据不符合格式");
			return INFO;
		}
		int step = 0;
		for (int i = 0; i < contents.length; i++) {
			try {
				paramsMap.put("logNo", contents[i].trim());
			} catch (Exception e) {
				addActionError("第" + (i + 1) + "行数据格式错误");
				continue;
			}

			try {
				resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Reject", paramsMap));
			} catch (Exception e) {
				LogUtil.exception("savelogic_json");
				addActionError("第" + (i + 1) + "行网络不通无法获取结果");
				continue;
			}

			if (!resultMap.getString("code").equals("0000")) {
				addActionError("第" + (i + 1) + "行失败,原因[" + resultMap.getString("message") + "]");
				continue;
			}
			addActionError("第" + (i + 1) + "行成功处理");
			step = step + 1;
		}
		addActionError("共成功 " + step + " 条数据");
		return SUCCESS;
	}

	public String view() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("logNo", logNo);
		paramMap.put("txnNo", StringUtil.split(txnNo));
		paramMap.put("logName", logName);
		paramMap.put("logOrder", logOrder);
		paramMap.put("logState", logState);
		paramMap.put("logDate", ParameterUtil.dateCombin(logDateTop, logDateEnd));
		paramMap.put("logRemark", logRemark);
		paramMap.put("crdNo", crdNo);
		paramMap.put("merNo", merNo);
		paramMap.put("txnName", txnName);
		paramMap.put("txnOrder", txnOrder);
		paramMap.put("txnSite", txnSite);
		paramMap.put("txnProduct", txnProduct);
		paramMap.put("txnNotify", txnNotify);
		paramMap.put("txnCur", txnCur);
		paramMap.put("txnLoc", txnLoc);
		paramMap.put("txnRate", txnRate);
		paramMap.put("txnAmount", txnAmount);
		paramMap.put("txnRemain", txnRemain);
		paramMap.put("txnRefund", txnRefund);
		paramMap.put("txnRefuse", txnRefuse);
		paramMap.put("txnFreeze", txnFreeze);
		paramMap.put("txnLogic", txnLogic);
		paramMap.put("txnSettle", txnSettle);
		paramMap.put("txnDanger", txnDanger);
		paramMap.put("txnVisible", txnVisible);
		paramMap.put("txnState", txnState);
		paramMap.put("txnDate", ParameterUtil.dateCombin(txnDateTop, txnDateEnd));
		paramMap.put("txnRemark", txnRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "logDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<VLogicTransaction> resultSet = logicService.relate(querySet);
		logicTransactionList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("logNo", logNo);
		paramMap.put("txnNo", StringUtil.split(txnNo));
		paramMap.put("logName", logName);
		paramMap.put("logOrder", logOrder);
		paramMap.put("logState", logState);
		paramMap.put("logDate", ParameterUtil.dateCombin(logDateTop, logDateEnd));
		paramMap.put("logRemark", logRemark);
		paramMap.put("crdNo", crdNo);
		paramMap.put("merNo", merNo);
		paramMap.put("txnName", txnName);
		paramMap.put("txnOrder", txnOrder);
		paramMap.put("txnSite", txnSite);
		paramMap.put("txnProduct", txnProduct);
		paramMap.put("txnNotify", txnNotify);
		paramMap.put("txnCur", txnCur);
		paramMap.put("txnLoc", txnLoc);
		paramMap.put("txnRate", txnRate);
		paramMap.put("txnAmount", txnAmount);
		paramMap.put("txnRemain", txnRemain);
		paramMap.put("txnRefund", txnRefund);
		paramMap.put("txnRefuse", txnRefuse);
		paramMap.put("txnFreeze", txnFreeze);
		paramMap.put("txnLogic", txnLogic);
		paramMap.put("txnSettle", txnSettle);
		paramMap.put("txnDanger", txnDanger);
		paramMap.put("txnVisible", txnVisible);
		paramMap.put("txnState", txnState);
		paramMap.put("txnDate", ParameterUtil.dateCombin(txnDateTop, txnDateEnd));
		paramMap.put("txnRemark", txnRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "logDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<VLogicTransaction> resultSet = logicService.relate(querySet);
		logicTransactionList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("关联交易号");
		fieldHead.add("物流公司");
		fieldHead.add("物流单号");
		fieldHead.add("状态");
		fieldHead.add("发生日期日期");
		fieldHead.add("发生日期时间");
		fieldHead.add("备注");
		fieldHead.add("卡编号");
		fieldHead.add("商户编号");
		fieldHead.add("名称");
		fieldHead.add("商户订单号");
		fieldHead.add("来源网站");
		fieldHead.add("产品信息");
		fieldHead.add("商户通知地址");
		fieldHead.add("原交易币种");
		fieldHead.add("原交易金额");
		fieldHead.add("实时汇率");
		fieldHead.add("交易金额");
		fieldHead.add("交易余额");
		fieldHead.add("退款状态");
		fieldHead.add("拒付状态");
		fieldHead.add("冻结状态");
		fieldHead.add("发货状态");
		fieldHead.add("结算状态");
		fieldHead.add("安全状态");
		fieldHead.add("隐藏状态");
		fieldHead.add("交易状态");
		fieldHead.add("发生日期日期");
		fieldHead.add("发生日期时间");
		fieldHead.add("交易备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (VLogicTransaction logicTransactionBean : logicTransactionList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(logicTransactionBean.getLogNo());
			fieldItem.add(logicTransactionBean.getTxnNo());
			fieldItem.add(logicTransactionBean.getLogName());
			fieldItem.add(logicTransactionBean.getLogOrder());
			fieldItem.add((logicTransactionBean.getLogState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(logicTransactionBean.getLogDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(logicTransactionBean.getLogDate(), DateUtil.TIME));
			fieldItem.add(logicTransactionBean.getLogRemark());
			fieldItem.add(logicTransactionBean.getCrdNo());
			fieldItem.add(logicTransactionBean.getMerNo());
			fieldItem.add(logicTransactionBean.getTxnName());
			fieldItem.add(logicTransactionBean.getTxnOrder());
			fieldItem.add(logicTransactionBean.getTxnSite());
			fieldItem.add(logicTransactionBean.getTxnProduct());
			fieldItem.add(logicTransactionBean.getTxnNotify());
			fieldItem.add(logicTransactionBean.getTxnCur());
			fieldItem.add(logicTransactionBean.getTxnLoc().toString());
			fieldItem.add(logicTransactionBean.getTxnRate().toString());
			fieldItem.add(logicTransactionBean.getTxnAmount().toString());
			fieldItem.add(logicTransactionBean.getTxnRemain().toString());
			fieldItem.add((logicTransactionBean.getTxnRefund().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnRefuse().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnFreeze().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnLogic().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnSettle().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnDanger().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnVisible().equals("0")) ? "无效" : "有效");
			fieldItem.add((logicTransactionBean.getTxnState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(logicTransactionBean.getTxnDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(logicTransactionBean.getTxnDate(), DateUtil.TIME));

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ILogicService getLogicService() {
		return logicService;
	}

	public void setLogicService(ILogicService logicService) {
		this.logicService = logicService;
	}

	public TLogic getLogicBean() {
		return logicBean;
	}

	public void setLogicBean(TLogic logicBean) {
		this.logicBean = logicBean;
	}

	public List<TLogic> getLogicList() {
		return logicList;
	}

	public void setLogicList(List<TLogic> logicList) {
		this.logicList = logicList;
	}

	public String getLogNo() {
		return logNo;
	}

	public void setLogNo(String logNo) {
		this.logNo = logNo;
	}

	public String getLogNos() {
		return logNos;
	}

	public void setLogNos(String logNos) {
		this.logNos = logNos;
	}

	public String getTxnNo() {
		return txnNo;
	}

	public void setTxnNo(String txnNo) {
		this.txnNo = txnNo;
	}

	public String getLogName() {
		return logName;
	}

	public void setLogName(String logName) {
		this.logName = logName;
	}

	public String getLogOrder() {
		return logOrder;
	}

	public void setLogOrder(String logOrder) {
		this.logOrder = logOrder;
	}

	public String getLogState() {
		return logState;
	}

	public void setLogState(String logState) {
		this.logState = logState;
	}

	public Date getLogDate() {
		return logDate;
	}

	public void setLogDate(Date logDate) {
		this.logDate = logDate;
	}

	public Date getLogDateTop() {
		return logDateTop;
	}

	public void setLogDateTop(Date logDateTop) {
		this.logDateTop = logDateTop;
	}

	public Date getLogDateEnd() {
		return logDateEnd;
	}

	public void setLogDateEnd(Date logDateEnd) {
		this.logDateEnd = logDateEnd;
	}

	public String getLogRemark() {
		return logRemark;
	}

	public void setLogRemark(String logRemark) {
		this.logRemark = logRemark;
	}

	public VLogicTransaction getLogicTransactionBean() {
		return logicTransactionBean;
	}

	public void setLogicTransactionBean(VLogicTransaction logicTransactionBean) {
		this.logicTransactionBean = logicTransactionBean;
	}

	public List<VLogicTransaction> getLogicTransactionList() {
		return logicTransactionList;
	}

	public void setLogicTransactionList(List<VLogicTransaction> logicTransactionList) {
		this.logicTransactionList = logicTransactionList;
	}

	public String getTxnNos() {
		return txnNos;
	}

	public void setTxnNos(String txnNos) {
		this.txnNos = txnNos;
	}

	public String getCrdNo() {
		return crdNo;
	}

	public void setCrdNo(String crdNo) {
		this.crdNo = crdNo;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getTxnName() {
		return txnName;
	}

	public void setTxnName(String txnName) {
		this.txnName = txnName;
	}

	public String getTxnOrder() {
		return txnOrder;
	}

	public void setTxnOrder(String txnOrder) {
		this.txnOrder = txnOrder;
	}

	public String getTxnSite() {
		return txnSite;
	}

	public void setTxnSite(String txnSite) {
		this.txnSite = txnSite;
	}

	public String getTxnProduct() {
		return txnProduct;
	}

	public void setTxnProduct(String txnProduct) {
		this.txnProduct = txnProduct;
	}

	public String getTxnNotify() {
		return txnNotify;
	}

	public void setTxnNotify(String txnNotify) {
		this.txnNotify = txnNotify;
	}

	public String getTxnCur() {
		return txnCur;
	}

	public void setTxnCur(String txnCur) {
		this.txnCur = txnCur;
	}

	public Double getTxnLoc() {
		return txnLoc;
	}

	public void setTxnLoc(Double txnLoc) {
		this.txnLoc = txnLoc;
	}

	public Double getTxnRate() {
		return txnRate;
	}

	public void setTxnRate(Double txnRate) {
		this.txnRate = txnRate;
	}

	public Double getTxnAmount() {
		return txnAmount;
	}

	public void setTxnAmount(Double txnAmount) {
		this.txnAmount = txnAmount;
	}

	public Double getTxnRemain() {
		return txnRemain;
	}

	public void setTxnRemain(Double txnRemain) {
		this.txnRemain = txnRemain;
	}

	public String getTxnRefund() {
		return txnRefund;
	}

	public void setTxnRefund(String txnRefund) {
		this.txnRefund = txnRefund;
	}

	public String getTxnRefuse() {
		return txnRefuse;
	}

	public void setTxnRefuse(String txnRefuse) {
		this.txnRefuse = txnRefuse;
	}

	public String getTxnFreeze() {
		return txnFreeze;
	}

	public void setTxnFreeze(String txnFreeze) {
		this.txnFreeze = txnFreeze;
	}

	public String getTxnLogic() {
		return txnLogic;
	}

	public void setTxnLogic(String txnLogic) {
		this.txnLogic = txnLogic;
	}

	public String getTxnSettle() {
		return txnSettle;
	}

	public void setTxnSettle(String txnSettle) {
		this.txnSettle = txnSettle;
	}

	public String getTxnDanger() {
		return txnDanger;
	}

	public void setTxnDanger(String txnDanger) {
		this.txnDanger = txnDanger;
	}

	public String getTxnVisible() {
		return txnVisible;
	}

	public void setTxnVisible(String txnVisible) {
		this.txnVisible = txnVisible;
	}

	public String getTxnState() {
		return txnState;
	}

	public void setTxnState(String txnState) {
		this.txnState = txnState;
	}

	public Date getTxnDate() {
		return txnDate;
	}

	public void setTxnDate(Date txnDate) {
		this.txnDate = txnDate;
	}

	public Date getTxnDateTop() {
		return txnDateTop;
	}

	public void setTxnDateTop(Date txnDateTop) {
		this.txnDateTop = txnDateTop;
	}

	public Date getTxnDateEnd() {
		return txnDateEnd;
	}

	public void setTxnDateEnd(Date txnDateEnd) {
		this.txnDateEnd = txnDateEnd;
	}

	public String getTxnRemark() {
		return txnRemark;
	}

	public void setTxnRemark(String txnRemark) {
		this.txnRemark = txnRemark;
	}

}
